﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using VAdvantage.Controls;
using VAdvantage.Grids;
using VAdvantage.Model;


namespace VAdvantage.Classes
{
    public class VControlFactory
    {
        /// <summary>
        ///Create control for Field.
        /// </summary>
        /// <param name="mField"></param>
        /// <param name="tableEditor"></param>
        /// <returns></returns>
        public static IControl GetControl(GridField mField, bool tableEditor)
        {
            return GetControl(null, mField, tableEditor, false, false);
            //return null;
        }   //  getEditor

        /// <summary>
        ///Create Control for MField.
        /// </summary>
        /// <param name="mTab"></param>
        /// <param name="mField"></param>
        /// <param name="tableEditor"></param>
        /// <returns></returns>
        public static IControl GetControl(GridTab mTab, GridField mField, bool tableEditor)
        {
            return GetControl(mTab, mField, tableEditor, false, false);
            //return null;
        }	//	getEditor


        public static IControl GetControl(GridTab mTab, GridField mField,
            bool tableEditor, bool disableValidation, bool n)
        {
            if (mField == null)
                return null;

            string columnName = mField.GetColumnName();
            bool mandatory = mField.IsMandatory(false);      //  no context check

            //  Not a Field
            if (mField.IsHeading())
                return null;
            bool isDisplayed = mField.IsDisplayed();

            IControl ctrl = null;
            int displayType = mField.GetDisplayType();
            bool isMandatory = mField.IsMandatory(false);
            bool isReadOnly = mField.IsReadOnly();
            bool isUpdateable = mField.IsEditable(false);
            int windowNo = mField.GetWindowNo();

            

            //	String (clear/password)
            if (displayType == DisplayType.String)
            {
                if (mField.IsEncryptedField())
                {
                    VAdvantage.Controls.VTextBox txt = new VAdvantage.Controls.VTextBox(columnName, isMandatory, isReadOnly, isUpdateable,
                                                    mField.GetDisplayLength(), mField.GetFieldLength(), "", mField.GetObscureType(), VAdvantage.Controls.VTextBox.TextType.Password, displayType);
                    txt.SetField(mField);
                    ctrl = txt;
                    //ctrl = new VTextBox();
                }
                else
                {
                    VAdvantage.Controls.VTextBox txt = new VAdvantage.Controls.VTextBox(columnName, isMandatory, isReadOnly, isUpdateable,
                                                    mField.GetDisplayLength(), mField.GetFieldLength(), "", mField.GetObscureType(), VAdvantage.Controls.VTextBox.TextType.Text, displayType);
                    txt.SetField(mField);
                    ctrl = txt;
                    //ctrl = new VTextBox();
                }
            }

            else if (displayType == DisplayType.Text)
            {
                //VAdvantage.Controls.VTextBox txt = new VAdvantage.Controls.VTextBox(columnName, isMandatory, isReadOnly, isUpdateable,
                //                       mField.GetDisplayLength(), mField.GetFieldLength(), "", mField.GetObscureType(), VAdvantage.Controls.VTextBox.TextType.MultiLine, displayType);
                //VAdvantage.Controls.VRichTextBox txt = new VAdvantage.Controls.VRichTextBox(columnName, isMandatory, isReadOnly, isUpdateable,
                //                       mField.GetDisplayLength(), mField.GetFieldLength(), displayType);
           //     txt.SetField(mField);
                ctrl = new VRichTextBox(columnName, isMandatory, isReadOnly, isUpdateable, mField.GetDisplayLength(), mField.GetFieldLength(), displayType);
                ctrl.SetField(mField);
            }

            else if (displayType == DisplayType.TextLong)
            {
                //VAdvantage.Controls.VRichTextBox txt = new VAdvantage.Controls.VRichTextBox(columnName, isMandatory, isReadOnly, isUpdateable,
                //                       mField.GetDisplayLength(), mField.GetFieldLength(), displayType);
                //txt.SetField(mField);
                //ctrl = txt;
                //ctrl = new VRichTextBox(columnName, isMandatory, isReadOnly, isUpdateable, 200, 20, displayType);
                ctrl = new VRichTextBox(columnName, isMandatory, isReadOnly, isUpdateable, mField.GetDisplayLength(),mField.GetFieldLength() , displayType);
                ctrl.SetField(mField);
            }

            else if (DisplayType.Amount == displayType || DisplayType.CostPrice == displayType || DisplayType.Quantity == displayType || DisplayType.Number == displayType)
            {
                VAdvantage.Controls.VAmountTextBox amt = new VAdvantage.Controls.VAmountTextBox(columnName, mandatory, isReadOnly, isUpdateable,
                                     displayType, mField.GetHeader());
                amt.SetRange(mField.GetValueMin(), mField.GetValueMax());
                amt.SetField(mField);
                ctrl = amt;
                //ctrl = new VAmountTextBox();
            }

            else if (DisplayType.Integer == displayType)//|| DisplayType.Quantity == displayType)
            {
                VAdvantage.Controls.VNumTextBox num = new VAdvantage.Controls.VNumTextBox(columnName, mandatory, isReadOnly, isUpdateable,
                                    displayType, mField.GetHeader());
                num.SetRange(mField.GetValueMin(), mField.GetValueMax());
                num.SetField(mField);
                ctrl = num;
                //ctrl = new  VNumTextBox();
            }
            else if (DisplayType.Number == displayType)
            {
                VAdvantage.Controls.VNumTextBox vt = new VAdvantage.Controls.VNumTextBox(columnName, mandatory, isReadOnly, isUpdateable,
                                    displayType, mField.GetHeader());
                vt.SetRange(mField.GetValueMin(), mField.GetValueMax());
                vt.SetField(mField);
                ctrl = vt;
                //ctrl = new VNumTextBox();

            }
           // Yes-No
            else if (displayType == DisplayType.YesNo)
            {
                VAdvantage.Controls.VCheckBox chk = new VAdvantage.Controls.VCheckBox(columnName, mandatory, isReadOnly, isUpdateable,
                    mField.GetHeader(), mField.GetDescription(), tableEditor);
                chk.SetDisplayType(displayType);
                chk.SetField(mField);
                ctrl = chk;
            }

           //     //	Date
            else if (DisplayType.IsDate(displayType))
            {
                if (displayType == DisplayType.DateTime && !DataBase.GlobalVariable.IsVisualEditor)
                    isReadOnly = true;

                VAdvantage.Controls.VDateTimePicker vd = new VAdvantage.Controls.VDateTimePicker(columnName, mandatory, isReadOnly, isUpdateable,
                                             displayType, mField.GetHeader());
                vd.SetField(mField);
                ctrl = vd;
                //ctrl = new VDateTimePicker();
            }


           //	Button
            else if (displayType == DisplayType.Button)
            {

                ///******3/1/2012 *******///
                ///implement Action pane 

                if (mField.GetIsLink())
                {
                    VAdvantage.Controls.VPaneItem itm = new VPaneItem();
                    itm.SetAttribute(mField, columnName, mandatory, isReadOnly, isUpdateable,
                                        mField.GetHeader(), mField.GetDescription(), mField.GetHelp(), mField.GetAD_Process_ID());
                    ctrl = itm;
                }
                else
                {
                    VAdvantage.Controls.VButton button = new VAdvantage.Controls.VButton();
                    button.SetAttribute(mField, columnName, mandatory, isReadOnly, isUpdateable,
                                        mField.GetHeader(), mField.GetDescription(), mField.GetHelp(), mField.GetAD_Process_ID());
                    ctrl = button;
                }
                

            }

            else if (displayType == DisplayType.Label)
            {
                ///******14/2/2012 *******///
                ///implement Action pane 
                ///

                VAdvantage.Controls.VTextBlock txt = new VTextBlock(columnName);
                //VAdvantage.Controls.VButton button = new VAdvantage.Controls.VButton();
                //button.SetAttribute(mField, columnName, mandatory, isReadOnly, isUpdateable,
                //                    mField.GetHeader(), mField.GetDescription(), mField.GetHelp(), mField.GetAD_Process_ID());
                txt.SetField(mField);
                ctrl = txt;
            }

            else if (displayType == DisplayType.Location || displayType == DisplayType.Locator)
            {

                VAdvantage.Controls.VTextBoxButton txt = new VAdvantage.Controls.VTextBoxButton(columnName,
                    isMandatory, isReadOnly, isUpdateable, displayType, mField.GetLookup());
                txt.SetField(mField);
                ctrl = txt;
            }

            else if (displayType == DisplayType.Account)
            {
                Controls.VAccount acct = new VAccount(columnName, mandatory, isReadOnly, isUpdateable, (MAccountLookup)mField.GetLookup(), mField.GetHeader());
                acct.SetField(mField);
                ctrl = acct;
            }
            else if (displayType == DisplayType.PAttribute)
            {
                VPAttribute attrib = new VPAttribute(columnName, mandatory, isReadOnly, isUpdateable, windowNo,
                (MPAttributeLookup)mField.GetLookup());
                attrib.SetField(mField);
                ctrl = attrib;
            }

            else if (DisplayType.IsLookup(displayType) || DisplayType.ID == displayType)
            {
                Model.Lookup lookup = mField.GetLookup();
                if (disableValidation && lookup != null)
                    lookup.DisableValidation();

                if (!disableValidation)
                {

                    if (displayType != DisplayType.Search)
                    {
                        VComboBox cmb = new VComboBox(columnName, mandatory, isReadOnly, isUpdateable, lookup, mField.GetDisplayLength());
                        cmb.SetDisplayType(displayType);
                        cmb.SetField(mField);
                        ctrl = cmb;
                        //ctrl = new VComboBox();
                    }
                    else
                    {
                        VAdvantage.Controls.VTextBoxButton txtb = new VTextBoxButton(columnName, mandatory, isReadOnly, isUpdateable, displayType, lookup);
                        txtb.SetField(mField);
                        ctrl = txtb;
                        //ctrl = new VTextBoxButton();
                    }
                }

                else
                {
                    if (lookup == null || (displayType != DisplayType.Search && lookup.GetDisplayType() != DisplayType.Search))
                    {
                        VComboBox cmb = new VComboBox(columnName, mandatory, isReadOnly, isUpdateable, lookup, mField.GetDisplayLength());
                        cmb.SetDisplayType(displayType);
                        cmb.SetField(mField);
                        ctrl = cmb;
                        // ctrl = new VComboBox();
                    }
                    else
                    {
                        displayType = DisplayType.Search;
                        VAdvantage.Controls.VTextBoxButton txtb = new VTextBoxButton(columnName, mandatory, isReadOnly, isUpdateable, displayType, lookup);
                        txtb.SetField(mField);
                        ctrl = txtb;
                        //ctrl = new  VTextBoxButton();
                    }


                }
            }

            else if (displayType == DisplayType.FileName || displayType == DisplayType.FilePath)
            {
                VFile file = new VFile(columnName, mandatory, isReadOnly, isUpdateable, displayType == DisplayType.FileName);
                file.SetField(mField);
                ctrl = file;
            }

            else if (displayType == DisplayType.Binary)
            {
                VBinary bin = new VBinary(columnName, windowNo);
                bin.SetField(mField);
                ctrl = bin;
            }

           //     //displayType == DisplayType.PrinterName 

            else if (displayType == DisplayType.URL)
            {
                VURL vs = new VURL(columnName, mandatory, isReadOnly, isUpdateable,
                    mField.GetDisplayLength(), mField.GetFieldLength());
                //vs.setName(columnName);
                vs.SetField(mField);
                ctrl = vs;
            }

           // else if (displayType == DisplayType.Assignment)
            // {
            //     //VAssignment assign = new VAssignment(columnName, mandatory, isReadOnly, isUpdateable);
            //     ////assign.setName(columnName);
            //     //assign.SetField(mField);
            //     //ctrl = assign;
            // }

           // //	Printer Name
            // else if (displayType == DisplayType.PrinterName)
            // {
            //     //VPrinter vs = new VPrinter(columnName, mandatory, isReadOnly, isUpdateable,
            //     //    mField.GetFieldLength());
            //     ////vs.setName(columnName);
            //     //vs.SetField(mField);
            //     //ctrl = vs;
            // }

           //     //  Image
            else if (displayType == DisplayType.Image)
            {
                VImage image = new VImage(columnName, windowNo);
                //image.setName(columnName);
                image.SetField(mField);
                ctrl = image;
            }



           // //else if (displayType == DisplayType.Assignment || displayType == DisplayType.Color || displayType == DisplayType.Image || displayType == DisplayType.PAttribute)
           // //{

           // //    Controls.VTextBox txt = new Controls.VTextBox();
           // //    txt.Text = "Assignment|Color|Image|PAttribute";
           // //    txt.Size = new System.Drawing.Size(300, 21);
           // //    txt.BackColor = System.Drawing.Color.BurlyWood;
           // //    txt.Name = columnName;
           // //    //if (readOnly || !updateable)
           // //    //{
           // //        txt.ReadOnly = true; //
           // //    //}
           // //    ctrl = txt;
           // //}
           // else if (displayType == DisplayType.Memo)
           // {
           //     VAdvantage.Controls.VRichTextBox txt = new VAdvantage.Controls.VRichTextBox(columnName, isMandatory, isReadOnly, isUpdateable,
           //                            mField.GetDisplayLength(), mField.GetFieldLength(), displayType);
           //     txt.SetField(mField);
           //     ctrl = txt;
           // }
            if (ctrl == null)
            {
                ctrl = new VTextBox(columnName,mandatory,isReadOnly,isUpdateable,40,20,null,"") ;
            }
            return ctrl;
        }

        internal static VLabel GetLabel(GridField mField)
        {
            if (mField == null)
                return null;

            int displayType = mField.GetDisplayType();

            //	No Label for FieldOnly, CheckBox, Button
            if (mField.IsFieldOnly()
                    || displayType == DisplayType.YesNo
                    || displayType == DisplayType.Button
                    || displayType == DisplayType.Label )
                return null;
            return new VLabel(mField.GetHeader(),mField.GetColumnName());
        }
    }
}
